package com.xr.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.sunnyday.constant.MessageConstant;
import com.xr.sunnyday.entity.PageResult;
import com.xr.sunnyday.entity.QueryPageBean;
import com.xr.sunnyday.entity.Result;
import com.xr.sunnyday.pojo.Setmeal;
import com.xr.sunnyday.service.SetmealService;
import com.xr.sunnyday.utils.QiNiuUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmaelService;

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmaelService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        setmaelService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);

    }
    @GetMapping("/findByid")
    public Result findByid(int setmealid){
        Setmeal setmeal = setmaelService.findByid(setmealid);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    @GetMapping("findCheckGroupBySetmel")
    public Result findCheckGroupBySetmel(int setmealid){
        List<Integer> checkgroup = setmaelService.findCheckGroupBySetmel(setmealid);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkgroup);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,int[] checkgroupIds){
        setmaelService.update(setmeal,checkgroupIds);
        return new Result(true,"成功");
    }

    @PostMapping("/delete")
    public Result delete(int setmealid){
        setmaelService.delete(setmealid);
        return new Result(true,"删除套餐表成功");
    }


    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        //获取图片名
        String originalFilename = imgFile.getOriginalFilename();
        //获取图片后缀名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成随机名
        String filename = UUID.randomUUID() + substring;
        //调用七牛
        try{
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),filename);

            Map<String,String> map = new HashMap<>();
            map.put("imgName",filename);
            map.put("domain",QiNiuUtils.DOMAIN);
            System.out.println(filename);
            System.out.println(QiNiuUtils.DOMAIN);
            return new Result(true,"成功",map);
        }catch (IOException e){
            e.printStackTrace();
        }
        return new Result(false,"失败");
    }



}
